//对Date的扩展，将 Date 转化为指定格式的String   
//月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，   
//年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)   
//例子：   
//(new Date()).format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423   
//(new Date()).format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18   
Date.prototype.format = function(fmt)   
{
	var o = {   
		"M+" : this.getMonth()+1,                 //月份   
		"d+" : this.getDate(),                    //日   
		"h+" : this.getHours(),                   //小时   
		"m+" : this.getMinutes(),                 //分   
		"s+" : this.getSeconds(),                 //秒   
		"q+" : Math.floor((this.getMonth()+3)/3), //季度   
		"S"  : this.getMilliseconds()             //毫秒   
	};   
	if(/(y+)/.test(fmt)) {
		fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
	}
	
	for(var k in o) {
		if(new RegExp("("+ k +")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
		}
	}
	return fmt;   
}  

function fnDatagridPrint(datagrid, formid, type) {
	
	var titls = '';
	var fields = '';
	var widths = '';
	var aligns = '';
	var param = '';
	var tableTitle = '';
	
	var total=$('#' + datagrid).datagrid('getPager').pagination("options").total;
	if(total==0){
		alert("页面中无查询结果!");
		return;
	}
	
	var frozenColumns = $('#' + datagrid).datagrid('options')['frozenColumns'][0];
	if(frozenColumns && frozenColumns.length > 0){
		for(var i=0; i<frozenColumns.length; i++){
			if(frozenColumns[i].title) {
				titls = titls + frozenColumns[i].title + ',';
				fields = fields + frozenColumns[i].field + ',';
				widths = widths + frozenColumns[i].width + ',';
				if(frozenColumns[i].align) {
					aligns = aligns + frozenColumns[i].align + ',';
				}
				else {
					aligns = aligns + 'left,';
				}
			}
		}
	}
	
	tableTitle = $('#' + datagrid).datagrid('options')['title'];
	var columns = $('#' + datagrid).datagrid('options')['columns'][0];
	if(columns && columns.length > 0){
		for(var i=0; i<columns.length; i++){
			if(columns[i].title) {
				titls = titls + columns[i].title + ',';
				fields = fields + columns[i].field + ',';
				widths = widths + columns[i].width + ',';
				if(columns[i].align) {
					aligns = aligns + columns[i].align + ',';
				}
				else {
					aligns = aligns + 'left,';
				}
			}
		}
	}

	param = titls.substring(0, titls.length-1) + "##" + fields.substring(0, fields.length-1) + "##" + widths.substring(0, widths.length-1) + "##" + aligns.substring(0, aligns.length-1) + "##" + tableTitle.substring(0, tableTitle.length);
	
	
	var tempForm = document.createElement("form");  
	tempForm.id = "dataGridForm";  
	tempForm.method = "post";  
	tempForm.action = $('#' + datagrid).datagrid('options')['url'];
	tempForm.target = '_blank';
	

	$(tempForm).append($('#'+formid).html());

	var rows;
	rows = document.createElement("input");  
	rows.type="hidden";  
	rows.name= "rows";
	var total=$('#' + datagrid).datagrid('getPager').pagination("options").pageSize;
	rows.value= total;
	$(tempForm).append(rows);
	
	var page;
	page = document.createElement("input");  
	page.type="hidden";  
	page.name= "page";
	var pageNumber=$('#' + datagrid).datagrid('getPager').pagination("options").pageNumber;
	page.value= pageNumber;
	$(tempForm).append(page);
	
	var sort;
	sort = document.createElement("input");  
	sort.type="hidden";  
	sort.name= "sort";
	sort.value= $('#' + datagrid).datagrid('options')['sortName'];
	$(tempForm).append(sort);
	
	var order;
	order = document.createElement("input");  
	order.type="hidden";  
	order.name= "order";
	order.value= $('#' + datagrid).datagrid('options')['sortOrder'];
	$(tempForm).append(order);
	
	var isPrint;
	isPrint = document.createElement("input");  
	isPrint.type="hidden";  
	isPrint.name= "isPrint";
	isPrint.value= "true";
	$(tempForm).append(isPrint);
	
	if(type){
		var reportExportType;
		reportExportType = document.createElement("input");  
		reportExportType.type="hidden";  
		reportExportType.name= "reportExportType";
		reportExportType.value= type;
		$(tempForm).append(reportExportType);
	}
	
	var printParam;
	printParam = document.createElement("input");  
	printParam.type="hidden";  
	printParam.name= "printParam";
	printParam.value= param;
	$(tempForm).append(printParam);
	
	//tempForm.attachEvent("onsubmit",function(){ openWindow(name); });
	document.body.appendChild(tempForm);  

	tempForm.fireEvent("onsubmit");
	tempForm.submit();
	document.body.removeChild(tempForm);
	
}



function fnDatagridPrintNoPage(datagrid, formid, type) {
	
	var titls = '';
	var fields = '';
	var widths = '';
	var aligns = '';
	var param = '';
	var tableTitle = '';
	
	var total=$('#' + datagrid).datagrid('getRows').length;
	if(total==0){
		alert("页面中无查询结果!");
		return;
	}
	
	var frozenColumns = $('#' + datagrid).datagrid('options')['frozenColumns'][0];
	if(frozenColumns && frozenColumns.length > 0){
		for(var i=0; i<frozenColumns.length; i++){
			if(frozenColumns[i].title) {
				titls = titls + frozenColumns[i].title + ',';
				fields = fields + frozenColumns[i].field + ',';
				widths = widths + frozenColumns[i].width + ',';
				if(frozenColumns[i].align) {
					aligns = aligns + frozenColumns[i].align + ',';
				}
				else {
					aligns = aligns + 'left,';
				}
			}
		}
	}
	
	tableTitle = $('#' + datagrid).datagrid('options')['title'];
	var columns = $('#' + datagrid).datagrid('options')['columns'][0];
	if(columns && columns.length > 0){
		for(var i=0; i<columns.length; i++){
			if(columns[i].title) {
				titls = titls + columns[i].title + ',';
				fields = fields + columns[i].field + ',';
				widths = widths + columns[i].width + ',';
				if(columns[i].align) {
					aligns = aligns + columns[i].align + ',';
				}
				else {
					aligns = aligns + 'left,';
				}
			}
		}
	}

	param = titls.substring(0, titls.length-1) + "##" + fields.substring(0, fields.length-1) + "##" + widths.substring(0, widths.length-1) + "##" + aligns.substring(0, aligns.length-1) + "##" + tableTitle.substring(0, tableTitle.length);
	
	
	var tempForm = document.createElement("form");  
	tempForm.id = "dataGridForm";  
	tempForm.method = "post";  
	tempForm.action = $('#' + datagrid).datagrid('options')['url'];
	tempForm.target = '_blank';
	

	$(tempForm).append($('#'+formid).html());

	var rows;
	rows = document.createElement("input");  
	rows.type="hidden";  
	rows.name= "rows";
	var total=$('#' + datagrid).datagrid('getRows').length;
	rows.value= total;
	$(tempForm).append(rows);
	
	var sort;
	sort = document.createElement("input");  
	sort.type="hidden";  
	sort.name= "sort";
	sort.value= $('#' + datagrid).datagrid('options')['sortName'];
	$(tempForm).append(sort);
	
	var order;
	order = document.createElement("input");  
	order.type="hidden";  
	order.name= "order";
	order.value= $('#' + datagrid).datagrid('options')['sortOrder'];
	$(tempForm).append(order);
	
	var isPrint;
	isPrint = document.createElement("input");  
	isPrint.type="hidden";  
	isPrint.name= "isPrint";
	isPrint.value= "true";
	$(tempForm).append(isPrint);
	
	if(type){
		var reportExportType;
		reportExportType = document.createElement("input");  
		reportExportType.type="hidden";  
		reportExportType.name= "reportExportType";
		reportExportType.value= type;
		$(tempForm).append(reportExportType);
	}
	
	var printParam;
	printParam = document.createElement("input");  
	printParam.type="hidden";  
	printParam.name= "printParam";
	printParam.value= param;
	$(tempForm).append(printParam);
	
	//tempForm.attachEvent("onsubmit",function(){ openWindow(name); });
	document.body.appendChild(tempForm);  

	tempForm.fireEvent("onsubmit");
	tempForm.submit();
	document.body.removeChild(tempForm);
	
}

function fnConfigOperation(bool)
{
	if(bool == true)
	{
		//配置是否使用屏蔽鼠标右键(不包含Input type="text"和textarea)
		window.document.oncontextmenu = function(){
			var oElement = window.event.srcElement;
			if(oElement.tagName == "INPUT"){
				if(oElement.type == "text"){
					return true;
				}
			}
			if(oElement.tagName == "TEXTAREA") {
				return true;
			}
			return false;
		};
		
		//配置是否使用屏蔽鼠标左键拖动(不包含Input type="text"和textarea)
		window.document.onselectstart = function(){
			var oElement = window.event.srcElement;
			if(oElement.tagName == "INPUT"){
				if(oElement.type == "text"){
					return true;
				}
			}
			if(oElement.tagName == "TEXTAREA") {
				return true;
			}
			return false;
		};
		
		//配置是否使用屏蔽F1帮助
		window.onhelp = function(){
			window.event.keyCode = 505;
			return false;
		};
	
		//配置是否使用屏蔽部分IE快捷键
		window.document.onkeydown = function(){  
			//屏蔽  Alt + 方向键 ←  
			//屏蔽  Alt + 方向键 →
			if((window.event.altKey) && ((window.event.keyCode==37) || (window.event.keyCode==39))){
				window.event.keyCode = 505;
				return false;
			}
		
			//屏蔽  BackSpace(不包含Input type="text"和textarea)
			if(window.event.keyCode==8){
				var oElement = window.event.srcElement;
				if(oElement.tagName == "INPUT"){
					if(oElement.type == "text"){
						return true;
					}
				}
				if(oElement.tagName == "TEXTAREA") {
					return true;
				}
				window.event.keyCode = 505;
				return false;
			}
			
			//屏蔽  F2 - F12 键
			if((window.event.keyCode>=113) && (window.event.keyCode<=123)){
				window.event.keyCode = 505;
		        return false; 
			}
			
			//屏蔽  Ctrl + (a - z (不包含c、v))键
			if(window.event.ctrlKey && ((window.event.keyCode>=65) && (window.event.keyCode<=90))){
				if((window.event.keyCode == 67) || (window.event.keyCode == 86)){
					return true;
				}
				window.event.keyCode = 505;
		        return false; 
			}
			
			//屏蔽  Ctrl + (F1 - F12)键
			if(window.event.ctrlKey && ((window.event.keyCode>=112) && (window.event.keyCode<=123))){
				window.event.keyCode = 505;
		        return false; 
			}
		};
	}
}

//***********************************************************************
//showSending
function showSending()
{
	/*if(top.window && top.window != window)
	{
		top.window.location.href = "javascript:fnLoadSending();";
	}*/
	
	var dialogHtml = '';
	dialogHtml += '<div id="mainDivSending">';
	dialogHtml += '<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"><tbody><tr>';
	dialogHtml += '<td align="right" width="35%"><img src="'+ $('#__systemctx').html() +'/resources/images/indicator_white.gif" width="16" height="16" /></td>';
	dialogHtml += '<td width="5%"></td>';
	dialogHtml += '<td align="left" width="60%">正在执行中, 请稍候...</td>';
	dialogHtml += '</tr></tbody></table></div>';
	//$('body').prepend(dialogHtml);

	$(dialogHtml).dialog({
		title: '提示信息',
		padding: 5,
		width: 400,
		height: 140,
		closed: false,
		cache: false,
		modal: true  
	});
	$('a', $('#mainDivSending').parent()).css('display','none');
	//focus iframe
	if(document.getElementById("mainDivSending")){
	document.getElementById("mainDivSending").focus();
	}
	else if(document.getElementById("mainDivSending")){
	document.getElementById("mainDivSending").focus();
	}
}

//closeSending
function closeSending()
{
	/*if(top.window && top.window != window)
	{
		top.window.location.href = "javascript:fnUnloadSending();";
	}*/
	
	if($('#mainDivSending')){
		$('a', $('#mainDivSending').parent()).click();
		window.document.body.removeChild($('#mainDivSending').parent().get(0));
	}
}

//为数值类型增加高精度计算方法
//加法
Number.prototype.add = function(arg){
    var r1,r2,m;
    try{r1=this.toString().split(".")[1].length}catch(e){r1=0}
    try{r2=arg.toString().split(".")[1].length}catch(e){r2=0}
    m=Math.pow(10,Math.max(r1,r2))
    return (this*m+arg*m)/m
}

//减法
Number.prototype.sub = function(arg){
    return this.add(-arg);
}

//乘法
Number.prototype.mul = function(arg){
    var m=0,s1=this.toString(),s2=arg.toString();
    try{m+=s1.split(".")[1].length}catch(e){}
    try{m+=s2.split(".")[1].length}catch(e){}
    return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
}

//除法
Number.prototype.div = function(arg){
    var t1=0,t2=0,r1,r2;
    try{t1=this.toString().split(".")[1].length}catch(e){}
    try{t2=arg.toString().split(".")[1].length}catch(e){}
    with(Math){
        r1=Number(this.toString().replace(".",""))
        r2=Number(arg.toString().replace(".",""))
        return (r1/r2)*pow(10,t2-t1);
    }
}
//验证是否浮点数,可以是正浮点数和零
function isPlusDouble(value) {
	var partrn = /^(\d+)(\.\d+)?$/;
	if (partrn.test(value)) {
		return true;
	} else {
		return false;
	}
};